package com.shunan.miaosha.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.shunan.miaosha.beans.Users;
import com.shunan.miaosha.dao.UserDao;
import com.shunan.miaosha.utils.MD5Util;

@Service
public class UserService {
	
	@Autowired
	private UserDao userdao;
	
	public Users findUserByID(int id){
		return userdao.findUserByID(id);
	}
	
	public int addUser(String name , String password){
		String salt=""+((int)(Math.random()*1000000));
		String pass=MD5Util.input2dbPassword(password,  salt);
		return userdao.insertUser(name, pass, salt);
	}
	
	/**
	 * d3b1294a61a07da9b49b6e22b2cbd7f9  123456+固定salt  前端编码完后
	 * @param name
	 * @param password
	 * @return
	 */
	public Users findUser(String name,String password){
		List<Users>users=findUsers(name);
		for(Users u:users){
			if(u.getName().equals(name)){
				String pass=MD5Util.input2dbPassword(password, u.getSalt());
				if(u.getPassword().equals(pass)){
					return u;
				}else{
					return null;
				}
			}
		}
		return null;	
	}
	
	public List<Users> findUsers(String name){
		return userdao.findUsers(name);
	}
}